// pages/standby/main.js
const WXAPI = require('apifm-wxapi')
const AUTH = require('../../utils/auth')
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        defAvatar: '/images/defAvatar.png',
        // statusBarHeight: app.globalData.statusBarHeight,
        //     titleBarHeight: app.globalData.titleBarHeight,
        companyId: undefined,
        userId: undefined,
        allList: [],
        cardList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // console.log(options);
        this.setData({
            ...options
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        //查询公司所有人和已选人
        let promise1 = WXAPI.getStandbyListCompany(this.data.companyId)
        let promise2 = WXAPI.getStandbyUser(this.data.userId)
        Promise.all([promise1, promise2]).then((values) => {
            // console.log('getStandbyListCompany:', values);
            if (values[0].code == 200 && values[1].code == 200) {
                let list = values[0].data
                let checked = []
                for (const item of [...values[1].data]) {
                    checked.push(item.userId)
                }
                for (const item of list) {
                    if (checked.indexOf(item.userId) >= 0) {
                        item.selected = true
                    } else {
                        item.selected = false
                    }
                }
                this.setData({
                    allList: JSON.parse(JSON.stringify(list)),
                    cardList: JSON.parse(JSON.stringify(list))
                })
            } else {
                wx.showToast({
                    title: '加载失败',
                    icon: "none"
                })
            }

        }).catch(err => {
            wx.showToast({
                title: '加载失败',
                icon: 'none',
            })
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    //选中
    checkCard(e) {
        let index = e.currentTarget.dataset.index
        let userId = e.currentTarget.dataset.userid
        let allList = JSON.parse(JSON.stringify(this.data.allList))
        let cardList = JSON.parse(JSON.stringify(this.data.cardList))

        function findIndex(item) {
            return item.userId == userId;
        }
        let i = allList.findIndex(findIndex)
        allList[i].selected = !allList[i].selected
        cardList[index].selected = !cardList[index].selected
        let count = 0
        allList.forEach(item => {
            if (item.selected) count++
        });
        if (count > 3) {
            wx.showToast({
                title: '最多选择3人',
                icon: 'error'
            })
        } else {
            this.setData({
                cardList: cardList,
                allList: allList
            })
        }
    },
    //确认
    submit() {
        let list = []
        let allList = [...this.data.allList]
        for (const item of allList) {
            if (item.selected) {
                list.push(item.userId)
            }
        }
        // console.log(list);
        if (list.length > 3) {
            return wx.showToast({
                title: '最多选择3人',
                icon: 'error'
            })
        }
        WXAPI.setStandbyUser(this.data.userId, list).then(res => {
            // console.log(res);
            if (res.code == 200) {
                wx.showToast({
                    title: '保存成功',
                    icon: 'success'
                })
            } else {
                wx.showToast({
                    title: '保存失败',
                    icon: 'error'
                })
            }
        }).catch(err => {
            wx.showToast({
                title: '保存失败',
                icon: 'error'
            })
        })
    },
    //搜索
    onConfirm(e) {
        let value = e.detail.value;
        this.setData({
            cardList: JSON.parse(JSON.stringify(this.data.allList.filter(item => item.userName.indexOf(value) > -1)))
        })
    }

})